/*
 * @Author: Liuxin 2849181826@qq.com
 * @Date: 2025-04-25 14:37:54
 * @LastEditors: Liuxin 2849181826@qq.com
 * @LastEditTime: 2025-04-25 16:31:22
 * @FilePath: \node-exe\electron-vite-vue\src\puppeteer_ads\mqtt\index.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/**
 * MQTT Service - Handles MQTT connection and messaging
 * Uses the main-process-adapter to receive MQTT messages from the Electron main process
 */

import { mqttService as mainProcessAdapter } from "./main-process-adapter.js"
import { windowsData, params } from "../config/index.js"
const { mode } = params
// MQTT服务配置
console.log("mode===>mqtt", mode)

// 获取设备id列表
const deviceIdList = windowsData?.deviceIdList || []

// MQTT服务实现 - 使用主进程适配器
export const mqttService = {
  client: mainProcessAdapter.client,
  isConnected: true, // 假定主进程已连接

  // 连接到MQTT服务器 - 在主进程中完成
  connect() {
    console.log("[MQTT Service] 连接由Electron主进程管理")
    return mainProcessAdapter.connect()
  },

  // 订阅主题 - 在主进程中完成
  subscribe(topics) {
    console.log(`[MQTT Service] 主题订阅由Electron主进程管理: ${topics}`)
    return mainProcessAdapter.subscribe(topics)
  },

  // 发布消息 - 通过主进程
  publish(topic, message, options = {}) {
    return mainProcessAdapter.publish(topic, message, options)
  },

  // 断开连接 - 在主进程中完成
  disconnect() {
    console.log("[MQTT Service] 断开连接由Electron主进程管理")
    return mainProcessAdapter.disconnect()
  },

  // 添加消息处理程序 - 简化迁移
  onMessage(callback) {
    console.log("[MQTT Service] 注册消息处理程序")
    return mainProcessAdapter.onMessage(callback)
  }
}
